# -*- coding: utf-8 -*-
# @Author  : 廖善武
# @Email   : liaosw18878@hundsum.com
# @Time    : 2018/5/15 17:52
# @File    : XmlToExcel.py
from xml.dom.minidom import parse
import collections
import xlwt
import os

def xmlToExcel(name,data):
    wb = xlwt.Workbook()
    ws = wb.add_sheet('sheet 1')
    row = 0
    ws.write(row, 0, '序号')
    ws.write(row, 1, '名称')
    ws.write(row, 2, '1')
    row += 1
    for key in data:
        ws.write(row,0,key)
        ws.write(row,2,data.get(key))
        row += 1
    wb.save('{0}.xls'.format(name))


def getInputData(xmlPath):
    InputData=collections.OrderedDict()
    UIExcept=collections.OrderedDict()
    DOMTree = parse(xmlPath)
    collection = DOMTree.documentElement
    if collection.hasAttribute('note'):
        print('执行案例'+ collection.getAttribute('note'))
    subs =collection.getElementsByTagName('sub')
    num = 1
    sub = subs[0]
    print('执行sub{{0}}:{1}'.format(num,sub.getAttribute('id')))
    num +=1
    ins = sub.getElementsByTagName('inparams')[0].getElementsByTagName('in')
    uis = sub.getElementsByTagName('UIexcept')[0].getElementsByTagName('in')
    for i in ins:
        InputData[i.getAttribute('name')] = i.getAttribute('value')
    for j in uis:
        UIExcept[j.getAttribute('name')] = j.getAttribute('value')
    return InputData

def getXmlFile(basePath):
    for root,dirs,files in os.walk(basePath):
        for file in files:
            if '.xml' == os.path.splitext(file)[1]:
                xmlPath = os.path.join(root,file)
                excelName = os.path.join(root,os.path.splitext(file)[0])
                print('解析：',xmlPath)
                try:
                    xmlToExcel(excelName,getInputData(xmlPath))
                except Exception as e:
                    print(e)

if __name__ == '__main__':
    xmlPath = r"D:\自动化测试\Blade\TestData\全面风险\监管报表\辅助查询\业务数据查询\融资融券负债查询.xml"
    xmlToExcel('D:\自动化测试\Blade\TestData\全面风险\监管报表\辅助查询\业务数据查询\融资融券负债查询', getInputData(xmlPath))
